//
//  KindViewController.m
//  ToYourHeartNews
//
//  Created by XJing on 16/1/22.
//  Copyright © 2016年 www.lanou3g.com 蓝鸥. All rights reserved.
//

#import "KindViewController.h"
#import "Videomodel.h"
#import "VideoTableViewCell.h"
#import "AfNetWorkingHandler.h"
#import <MJRefresh.h>
@interface KindViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong)UITableView *tableView;
@property (nonatomic, strong)NSMutableArray *dataArr;
@property (nonatomic, assign)NSInteger number;
@end

@implementation KindViewController
- (void)dealloc
{
    [_sid release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.tableView = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    [_tableView registerClass:[VideoTableViewCell class] forCellReuseIdentifier:@"kindCell"];
    [self.view addSubview:_tableView];
    [_tableView release];
    //数据处理
    [self dataHandler];
    //下拉刷新
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(dataHandler)];
    [_tableView.mj_header beginRefreshing];
    
    // 上拉加载
    self.tableView.mj_footer = [MJRefreshAutoFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadnewdata)];
    if (_dataArr.count != 0) {
        [_tableView.mj_footer beginRefreshing];
    }
    self.number = 10;
    
}
//上拉加载
- (void)loadnewdata
{
    NSString *str = [NSString stringWithFormat:@"http://c.m.163.com/nc/video/list/%@/y/%ld-10.html",_sid, _number];
    [AfNetWorkingHandler GetWithStr:str complection:^(id result) {
        NSArray *arr = [result objectForKey:_sid];
        NSArray *arrTemp = [NSArray array];
        arrTemp = [Videomodel modelHanderWithArray:arr];
        for (NSInteger i = 0; i < 10; i++) {
            [self.dataArr addObject:arrTemp[i]];
        }
        [_tableView reloadData];
    }];
    [_tableView.mj_footer endRefreshing];
    self.number += 10;
}

//数据处理
-(void)dataHandler
{
    NSString *str = [NSString stringWithFormat:@"http://c.m.163.com/nc/video/list/%@/y/0-10.html",_sid];
//    self.dataArr = [NSMutableArray array];
    [AfNetWorkingHandler GetWithStr:str complection:^(id result) {
        //
        NSLog(@"%@", _sid);
        NSLog(@"result:%@", result);
        NSArray *arr = [result objectForKey:_sid];
        self.dataArr = [Videomodel modelHanderWithArray:arr];
        [_tableView reloadData];
    }];
    [_tableView.mj_header endRefreshing];
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 300;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArr.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    VideoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"kindCell"];
    Videomodel *m = _dataArr[indexPath.row];
    cell.titleLable.text = m.title;
    cell.detailLable.text = m.description1;
    //将时间转换成00:00形式
    NSNumber *n = m.length;
    if (n.integerValue < 60) {
        cell.timeLable.text = [NSString stringWithFormat:@"00: %ld", n.integerValue];
    }else{
        NSInteger fen = n.integerValue / 60;
        NSInteger miao = n.integerValue - 60 * fen;
        NSString *time = [NSString stringWithFormat:@"%ld : %ld", fen, miao];
        cell.timeLable.text = time;
    }
    //将播放次数转换成0.0万形式
    NSNumber *play = m.playCount;
    NSInteger wan = play.integerValue / 10000;
    NSInteger qian = (play.integerValue - wan * 10000) / 1000;
    cell.playLable.text = [NSString stringWithFormat:@"%ld.%ld万", wan, qian];
    cell.str = m.mp4_url;
    cell.commentLable.text = [m.replyCount.stringValue stringByAppendingString:@"评论"];
    return cell;
    return cell;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
